/*
 * @Copy.Right (c)2019 greatgas.cn
 *
 * @CreateTime 2019年7月3日 下午2:10:56
 */
package com.personal.system.utils;

import com.personal.system.entity.approval.UserTaskVo;
import org.activiti.bpmn.model.*;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Activiti工具集
 */
@Service("activitiUtils")
public class ActivitiUtils {

//    @Autowired
//    private FlowProcessService flowProcessService;
//
//    /**
//     * 获取当前任务所在的流程实例创建人ID
//     * @param execution
//     * @return
//     */
//    public String getStartUserId(DelegateExecution execution) {
//        String processInsId = execution.getProcessInstanceId();
//        ProcessInstance processInstance = flowProcessService.queryProcessInstanceById(processInsId);
//
//        String startUserId = processInstance.getStartUserId();
//        return StringUtil.isNotBlank(startUserId) ? startUserId : null;
//    }
//
    /**
     * 创建用户任务
     * @param id 任务id
     * @return
     */
    public static UserTask createUserTask(String id, UserTaskVo vo) {
        UserTask userTask = new UserTask();
        userTask.setName(vo.getTaskName());
        userTask.setId(id);
        userTask.setAssignee(vo.getAssignee());
        userTask.setCategory(vo.getCategory());
        return userTask;
    }

    /**
     * 连接线
     * @param from 开始任务id
     * @param to 连接任务id
     * @return
     */
    public static SequenceFlow createSequenceFlow(String from, String to, String name, String condition) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        flow.setName(name);
        if (!StringUtils.isEmpty(condition)) {
            flow.setConditionExpression(condition);
        }
        return flow;
    }

    public static SequenceFlow createSequenceFlow(String from, String to) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        return flow;
    }

    /**
     * 开始任务
     * @return
     */
    public static StartEvent createStartEvent() {
        StartEvent startEvent = new StartEvent();
        startEvent.setId("start");
        return startEvent;
    }

    /**
     * 结束任务
     * @return
     */
    public static EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId("end");
        return endEvent;
    }

    public static ExclusiveGateway createExclusiveGateway(String id) {
        ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
        exclusiveGateway.setId(id);
        return exclusiveGateway;
    }

}
